package iscript.gameFramework.IsoEngine.display
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import iscript.gameFramework.IsoEngine.data.IsoConfig;
	import iscript.gameFramework.IsoEngine.util.IsoSkew;
	import iscript.gameFramework.IsoEngine.util.IsoUtil;
	import iscript.uiFramework.baseShape.Ball;
	
	public class IsoBox extends IsoObject
	{
		/**
		 * FacePoint
		 * ◇  点的顺序  top right bottom left
		 */
		private var bottomFacePoint:Vector.<Point>;
		private var frontFacePoint:Vector.<Point>;
		private var topFacePoint:Vector.<Point>;
		private var backFacePoint:Vector.<Point>;
		private var leftFacePoint:Vector.<Point>;
		private var rightFacePoint:Vector.<Point>;
		
		private var _topBitmap:BitmapData;
		private var _frontBitmap:BitmapData;
		private var _rightBitmap:BitmapData;
		
		//sin
		private var _degreesTop_Right:Number = 0.4472135954999579;
		private var _degreesTop_Left:Number = 0.4472135954999579;
		
		private var _container:Sprite;
		
		public function IsoBox(width:Number = 0, length:Number = 0, height:Number = 0)
		{
			
			super(0, width, length, height);
			_innerIsoOffX = -IsoConfig.IsoGridSize >> 1;
			_innerIsoOffZ = -IsoConfig.IsoGridSize >> 1;			
			draw();
			addChild(new Ball(5));
		}
		
		public function setBoxSize(width:Number = 0, length:Number = 0, height:Number = 0):void
		{
			_width = width;
			_length = length;
			_height = height;
			draw();
		}
		
		override public function get width():Number
		{
			return super.width;
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			_width = value;
		}
		
		override public function get height():Number
		{
			return super.height;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			draw();
		}
		
		override protected function draw():void
		{
			for (var i:int = numChildren - 1; i >= 0; i--)
			{
				removeChildAt(i);
			}
			bottomFacePoint = new Vector.<Point>();
			topFacePoint = new Vector.<Point>();
			frontFacePoint = new Vector.<Point>();
			backFacePoint = new Vector.<Point>();
			leftFacePoint = new Vector.<Point>();
			rightFacePoint = new Vector.<Point>();
			createBottomFace();
			createLeftFace();
			createBackFace();
			_container = new Sprite();
			addChild(_container);
			createRightFace();
			createFrontFace();
			createTopFace();
			updateScreenPosition();
		}
		
 
		
		
		public function createBottomFace():void
		{
			
			if (_width == 0)
			{
				bottomFacePoint.push(new Point(0, 0)); //Top    Point
				bottomFacePoint.push(new Point(_size, _size >> 1)); //Right  Point
				bottomFacePoint.push(new Point(0, _size)); //Bottom Point
				bottomFacePoint.push(new Point(-_size, _size >> 1)); //Left   Point
			}
			else
			{
				var topPoint:Point = new Point(0, 0);	
				var rightHeight:Number = _degreesTop_Right * _width;
				var rightWidth:Number = Math.sqrt(_width * _width - rightHeight * rightHeight);
				var rightPoint:Point = new Point(rightWidth, rightHeight);				
				var leftHeight:Number = _degreesTop_Left * _length;
				var leftWidth:Number = -Math.sqrt(_length * _length - leftHeight * leftHeight);
				var leftPoint:Point = new Point(leftWidth, leftHeight);				
				var bottomPoint:Point = new Point(leftWidth + rightWidth, leftHeight + rightHeight)
				bottomFacePoint.push(topPoint); //Top    Point
				bottomFacePoint.push(rightPoint); //Right  Point
				bottomFacePoint.push(bottomPoint); //Bottom Point
				bottomFacePoint.push(leftPoint); //Left   Point
				
			}
			
			createFace("bottom", bottomFacePoint);
		
		}
		
		public function createLeftFace():void
		{
			leftFacePoint.push(new Point(bottomFacePoint[3].x, bottomFacePoint[3].y - _height));
			leftFacePoint.push(new Point(bottomFacePoint[0].x, bottomFacePoint[0].y - _height));
			leftFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y));
			leftFacePoint.push(new Point(bottomFacePoint[3].x, bottomFacePoint[3].y));
			createFace("left", leftFacePoint);
		}
		
		public function createBackFace():void
		{
			backFacePoint.push(new Point(bottomFacePoint[0].x, bottomFacePoint[0].y - _height));
			backFacePoint.push(new Point(bottomFacePoint[1].x, bottomFacePoint[1].y - _height));
			backFacePoint.push(new Point(bottomFacePoint[1].x, bottomFacePoint[1].y));
			backFacePoint.push(new Point(bottomFacePoint[0].x, bottomFacePoint[0].y));
			createFace("back", backFacePoint);
		}
		
		public function createFrontFace():void
		{
			frontFacePoint.push(new Point(bottomFacePoint[3].x, bottomFacePoint[3].y - _height)); //Top Left
			frontFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y - _height)); //Top Right
			frontFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y)); //Bottom Right
			frontFacePoint.push(new Point(bottomFacePoint[3].x, bottomFacePoint[3].y)); //Bottom Left
			
			createFace("front", frontFacePoint);
		}
		
		public function createRightFace():void
		{
			rightFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y - _height));
			rightFacePoint.push(new Point(bottomFacePoint[1].x, bottomFacePoint[1].y - _height));
			rightFacePoint.push(new Point(bottomFacePoint[1].x, bottomFacePoint[1].y));
			rightFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y));
			
			createFace("right", rightFacePoint);
		}
		
		public function createTopFace():void
		{
			topFacePoint.push(new Point(bottomFacePoint[0].x, bottomFacePoint[0].y - _height));
			topFacePoint.push(new Point(bottomFacePoint[1].x, bottomFacePoint[1].y - _height));
			topFacePoint.push(new Point(bottomFacePoint[2].x, bottomFacePoint[2].y - _height));
			topFacePoint.push(new Point(bottomFacePoint[3].x, bottomFacePoint[3].y - _height));
			
			createFace("top", topFacePoint);
		}
		
		private function createFace(faceName:String, points:Vector.<Point>):void
		{
			var container:Sprite = new Sprite();
			container.name = faceName;
			container.graphics.clear();
			container.graphics.beginFill(Math.random() * 0xFFFFFF, 0);
			//container.graphics.beginFill(Math.random()*0xFFFFFF,Math.random());
			container.graphics.lineStyle(1, 0x000000, 1);
			container.graphics.moveTo(points[0].x, points[0].y); //Top    Point
			container.graphics.lineTo(points[1].x, points[1].y); //Right  Point
			container.graphics.lineTo(points[2].x, points[2].y); //Bottom Point
			container.graphics.lineTo(points[3].x, points[3].y); //Left   Point
			container.graphics.endFill();
			addChild(container);
		}
		
		public function get topBitmap():BitmapData
		{
			return _topBitmap;
		}
		
		public function set topBitmap(value:BitmapData):void
		{
			_topBitmap = value;
			if (value)
			{
				setFaceBitmap("top", value);
			}
			else
			{
				
			}
		
		}
		
		public function get frontBitmap():BitmapData
		{
			return _frontBitmap;
		}
		
		public function set frontBitmap(value:BitmapData):void
		{
			_frontBitmap = value;
			if (value)
				setFaceBitmap("front", value)
		}
		
		public function get rightBitmap():BitmapData
		{
			return _rightBitmap;
		}
		
		public function set rightBitmap(value:BitmapData):void
		{
			_rightBitmap = value;
			if (value)
				setFaceBitmap("right", value)
		}
		
		/**
		 * 内容容器
		 */
		public function get container():Sprite 
		{
			return _container;
		}
		
		private function setFaceBitmap(faceName:String, bd:BitmapData):void
		{
			faceName = faceName.toLocaleLowerCase();
			var target:Sprite = Sprite(getChildByName(faceName));
			if (target == null)
				return;
			for (var i:int = target.numChildren - 1; i >= 0; i--)
			{
				target.removeChildAt(i);
			}
			
			var skew:IsoSkew = new IsoSkew(target, bd);
			switch (faceName)
			{
				case "top": 
					skew.setTransformPoints(topFacePoint[0], topFacePoint[1], topFacePoint[2], topFacePoint[3]);
					break;
				case "bottom": 
					skew.setTransformPoints(bottomFacePoint[0], bottomFacePoint[1], bottomFacePoint[2], bottomFacePoint[3]);
					break;
				case "left": 
					skew.setTransformPoints(leftFacePoint[0], leftFacePoint[1], leftFacePoint[2], leftFacePoint[3]);
					break;
				case "right": 
					skew.setTransformPoints(rightFacePoint[0], rightFacePoint[1], rightFacePoint[2], rightFacePoint[3]);
					break;
				case "front": 
					skew.setTransformPoints(frontFacePoint[0], frontFacePoint[1], frontFacePoint[2], frontFacePoint[3]);
					break;
				case "back": 
					skew.setTransformPoints(backFacePoint[0], backFacePoint[1], backFacePoint[2], backFacePoint[3]);
					break;
			}
		}
	
	}

}